<?php


namespace app\api\controller;


use app\admin\model\ClinicLog;
use app\common\controller\Api;
use app\common\controller\Backend;
use think\Db;

class Store  extends Api
{
    protected $noNeedLogin = ['getList'];
    protected $noNeedRight = '*';


    /**
     * 检查当前用户在当前门店的活动中是否已经报名
     */
    public function user_is_baoming()
    {
        $store_id = $this->request->post('store_id');//门店ID
        $clinc_activity_id = $this->request->post('clinc_activity_id');
        $clinc = ClinicLog::where(['user_id'=>$this->auth->id,'clinc_score_id'=>$store_id,'clinc_activity_id'=>$clinc_activity_id])->find();
        $is_baoming = false;
        if($clinc){
        $is_baoming = true;
        }
        $this->success('获取成功',['is_baoming'=>$is_baoming,'clinc'=>$clinc]);
    }


    /*
     * 获取门店列表接口
     * */
    public function getList()
    {
        $store_list = \app\admin\model\shopro\store\Store::where(['status'=>1])->order('id','desc')->paginate();
        $this->success('获取门店成功',$store_list);
    }

    /*
     * 通过id获取详情信息
     * */
    public function getByID()
    {
        $id = $_POST['id'];


        $res = Db::query("select * from fa_shopro_store where id='$id' order  by id  desc ");

        if (count($res) > 0) {
            $list = [];
            foreach ($res as $val) {
                $id = $val['id'];
                $name = $val['name'];
                $pro_name = $val['province_name'];
                $city_name = $val['city_name'];
                $area_name = $val['area_name'];
                $address = $val['address'];
                //获取活动
                $res1 = Db::query("select * from fa_huodong where  store_id='$id'  order  by id limit 1");
                $list1 = [];
                foreach ($res1 as $val1) {
                    $huodong_id = $val1['id'];

                    $title = $val1['title'];
                    $start_time = $val1['start_time'];
                    $end_time = $val1['end_time'];
                    $addr = $val1['addr'];
                    $doctor_ids = explode(",", $val1['doctor_ids']);
                    //获取问询医生
                    $doctor_len = count($doctor_ids);
                    $doctor_list = [];
                    for ($i = 0; $i < $doctor_len; $i++) {
                        $doctor_id = $doctor_ids[$i];
                        $res4 = Db::query("select * from fa_doctor where id='$doctor_id'");
                        foreach ($res4 as $val4) {
                            $arr4['doctor_position'] = $val4['doctor_position'];
                            $arr4['doctor_name'] = $val4['doctor_name'];
                            $arr4['doctor_mobile'] = $val4['doctor_mobile'];
                            $arr4['hospital_name'] = $val4['hospital_name'];
                            $arr4['goodat'] = $val4['goodat'];
                            $arr4['doctor_tags'] = $val4['doctor_tags'];
                            $arr4['image'] = $val4['image'];
                            array_push($doctor_list, $arr4);
                        }

                    }

                    $add_time = date('Y-m-d h:i:s');
                    //获取报名人数
                    $res3 = Db::query("select * from fa_huodong_baoming where huodong_id='$huodong_id' order  by id desc ");
                    $list2 = [];
                    foreach ($res3 as $val3) {
                        $name3 = $val3['name'];
                        $phone3 = $val3['phone'];
                        $add_time3 = $val3['add_time'];
                        $arr3 = [
                            'name' => mb_substr($name3, 0, 1) . "**",
                            'phone' => mb_substr($phone3, 0, 3) . "****" . mb_substr($phone3, 8, 11),
                            'add_time' => $add_time3
                        ];
                        array_push($list2, $arr3);
                    }


                    $arr1 = [
                        'huodong_id' => $id,
                        'title' => $title,
                        'start_time' => $start_time,
                        'end_time' => $end_time,
                        'addr' => $addr,
                        //                         'add_time'=>$add_time,
                        'menber' => $list2,
                        'doctor_list' => $doctor_list
                    ];
                    array_push($list1, $arr1);

                }

                $arr = [
                    'store_id' => $id,
                    'name' => $name,
                    'pro_name' => $pro_name,
                    'city_name' => $city_name,
                    'area_name' => $area_name,
                    'address' => $address,
                    'info' => $list1,

                ];
                array_push($list, $arr);

            }

            $data = [
                'code' => 1,
                'msg' => 'ok',
                'data' => $list
            ];

        } else {
            $data = [
                'code' => 0,
                'msg' => 'err',
                'data' => null
            ];
        }

        echo json_encode($data);


    }

}